package com.dbappsecurity.bifrost.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.dbapp.uilib.widget.titlebar.TitleBar;
import com.dbappsecurity.bifrost.R;
import com.dbappsecurity.jsbridge.entity.jsThirdTitle;
import com.google.zxing.Result;
import com.google.zxing.client.android.AutoScannerView;
import com.google.zxing.client.android.BaseCaptureActivity;
import java.util.Objects;
import org.parceler.Parcels;

import static com.dbapp.uilib.widget.titlebar.TitleBar.OptionItem.TYPE_IMG_NET;
import static com.dbapp.uilib.widget.titlebar.TitleBar.POS_LEFT;

public class QRActivity extends BaseCaptureActivity {

  @BindView(R.id.toolbar) TitleBar mTitleBar;

  @BindView(R.id.preview_view) SurfaceView previewView;

  @BindView(R.id.autoscanner_view) AutoScannerView autoscannerView;

  jsThirdTitle mJsThirdTitle;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qr);
    ButterKnife.bind(this);
    mJsThirdTitle = Parcels.unwrap(
        Objects.requireNonNull(getIntent().getExtras()).getParcelable("jsThirdTitle"));
    if (mJsThirdTitle==null) return;
    mTitleBar.setTitle(mJsThirdTitle.getTitle());
    mTitleBar.setBackgroundColor(mJsThirdTitle.getNavBackColor());
    TitleBar.OptionItem
        optionItem = mTitleBar.addItem(POS_LEFT,TYPE_IMG_NET,mJsThirdTitle.getBackIcon(),false);
    optionItem.setOnClickListener(v -> finish());
  }

  @Override
  protected void onResume() {
    super.onResume();
    autoscannerView.setCameraManager(cameraManager);
  }

  @Override
  public SurfaceView getSurfaceView() {
    return (previewView == null) ? (SurfaceView) findViewById(R.id.preview_view) : previewView;
  }

  @Override
  public void dealDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
    Log.i("QRActivity", "Arirus dealDecode: ");
    Log.i("ds", "dealDecode ~~~~~ " + rawResult.getText() + " " + barcode + " " + scaleFactor);

    Intent intent = new Intent();
    intent.putExtra("content", rawResult.getText());
    setResult(RESULT_OK, intent);
    finish();
  }
}
